home *** CD-ROM | disk | FTP | other *** search
- Path: pegasus.montclair.edu!harmon
- From: harmon@pegasus.montclair.edu (Derek Harmon)
- Newsgroups: comp.lang.c
- Subject: Re: Got Questions About Multi-Dimension Arrays (After Reading FAQ)
- Date: 1 Feb 1996 01:18:41 -0500
- Organization: Montclair State University
- Message-ID: <harmon.823155385@pegasus.montclair.edu>
- References: <4ep87b$o60@alcor.usc.edu>
- NNTP-Posting-Host: pegasus.montclair.edu
- X-Newsreader: NN version 6.5.0 #68 (NOV)
-
-
- ** Quoting a message by <wawda@alcor.usc.edu> dated <31-Jan-1996>:
-
- > why it isn't possible to do this:
- >
- > void myfunc(int *array,int rows,int cols)
- > {
- > printf("%d\n",array[1][1]);
- > }
-
- int *array is a pointer to an integer, ONE integer. You can't use
- the []'s on an integer, so the compiler will complain about it. Doesn't
- matter if the name of your formal parameter (in the function heading)
- matches a global variable.. as far as C is concerned, this function
- expects a pointer to an integer, and two ints.
-
- >
- > and call the function like this:
- >
- > int m[2][2] = {1, 2, 3, 4};
-
- I always m[2][2] = { {1, 2}, {3, 4} }; else it gets very confusing.
- >
- > myfunc(m,2,2);
-
- Since the array name m devolves into a pointer to an int which is
- the first element of the array, *array = 1 in the function when called
- in this way.
-
- > So why can't C convert from pointer to integer to a multidemsional
- > array. The reason I want to do this is because my function needs to
- > take in an array of any width and height. Thank you for your time,
-
- A dynamic array huh? You need to think in pointers (note the
- plurality) Arrays are really pointers, the more dimensions, the more
- pointers you need.
-
- Try,
-
- : void yourfunc(int *array[], int rows, int cols)
-
- The (int *)array can be indexed with one [] index in your function,
- and then you can use the other [] to index rows (or is it columns?) :)
- What it really comes down to is int **array, a double pointer! Think
- about it. C is full of hobgoblins, most are more gruesome though.
-
- -- Stone
- --
- ... Engineers never die, they just lose their tolerance.
-
-
-